package com.homestead.utils;

import com.homestead.constant.Constant;
import com.homestead.exception.ResponseErrorCode;
import com.homestead.resp.CommonResponse;

/**
 * @author yhb
 * @date 2021-03-26 9:11
 */
public class CommonResponseUtil {
    public static <T> CommonResponse<T> success(){
        return success(null, null);
    }

    public static <T> CommonResponse<T> successMessage(String message){
        return success(message, null);
    }

    public static <T> CommonResponse<T> successObj(T data){
        return success(Constant.SUCCESS, data);
    }

    public static <T> CommonResponse<T> success(String message, T data){
        return new CommonResponse<>(Constant.SUCCESS_CODE, message, data);
    }

    public static <T> CommonResponse<T> fail(ResponseErrorCode errorCode) {
        return fail(errorCode.getCode(), errorCode.getMessage());
    }

    public static <T> CommonResponse<T> fail(ResponseErrorCode errorCode, T data) {
        return newResult(errorCode.getCode(), errorCode.getMessage(), data);
    }

    public static <T> CommonResponse<T> fail(String code, String message) {
        return new CommonResponse<>(code, message, null);
    }

    public static <T> CommonResponse<T> newResult(String code, String message, T data) {
        return new CommonResponse<>(code, message, data);
    }
}
